package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Transactional
    public void saveWithFlavor(DishDto dishDto){
        //将dish的有关信息保存到dish表中
        this.save(dishDto);
        //从里面获取id
        Long disId = dishDto.getId();
        //获取flavors的集合
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(disId);
            return item;
        }).collect(Collectors.toList());
        //保存dishflavor表中
        dishFlavorService.saveBatch(flavors);
    }

    public DishDto getByIdWithFlavor(Long id){
        //获取当前id对应的Dish对象
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);
        //获取当前id对应的DishFlavor对象
        LambdaQueryWrapper<DishFlavor> lqw =new LambdaQueryWrapper<>();
        lqw.eq(id!=null,DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList= dishFlavorService.list(lqw);
        //存入集合
        dishDto.setFlavors(dishFlavorList);
        return dishDto;

    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作

        LambdaQueryWrapper<DishFlavor> lqw =new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(lqw);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

//        //更新dish表基本信息
//        this.updateById(dishDto);
//
//        LambdaQueryWrapper<DishFlavor> lqw =new LambdaQueryWrapper();
//        lqw.eq(DishFlavor::getDishId,dishDto.getId());
//        dishFlavorService.remove(lqw);
//        //添加当前提交过来的口味数据---dish_flavor表的insert操作
//        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
//
//        dishFlavorList=dishFlavorList.stream().map((item)->{
//            item.setDishId(dishDto.getId());
//            return item;
//        }).collect(Collectors.toList());
//        dishFlavorService.saveBatch(dishFlavorList);

    }

}
